package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    //菜品分页查询
    @GetMapping("page")
    public Result<PageResult> page(DishPageQueryDTO dishdto){

        log.info("分页查询,参数：{}",dishdto.toString());

        PageResult pageinfo = dishService.list(dishdto);

        return Result.success(pageinfo);

    }


//    修改菜品
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dishDTO){

        dishService.updateDish(dishDTO);

        return Result.success();
    }


//    批量删除菜品
    @DeleteMapping
    public Result removeById(Long[] ids){
        dishService.removeById(ids);
        return Result.success();
    }


//    新增菜品
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO){
        log.info(dishDTO.toString());
        dishService.save(dishDTO);
        return Result.success();
    }


//    根据id查询菜品
    @GetMapping("/{id}")
    public Result<DishVO> selectById(@PathVariable Long id){
        DishVO dishVO = dishService.selectById(id);

        if (dishVO != null){
            return Result.success(dishVO);
        }
        return Result.error("失败");
    }

//    根据分类id查询菜品
    @GetMapping("/list")
    public Result<List<Dish>> selectByCateId(Long categoryId){
        List<Dish> dish = dishService.selectByCateId(categoryId);
        return Result.success(dish);
    }


//    菜品起售、停售
    @PostMapping("/status/{status}")
    public Result updateStatus(Long id,@PathVariable Integer status){

        try {
            dishService.updateStatus(id, status);
        }catch (Exception e){
            return Result.error(e.toString());
        }

        return Result.success();
    }

}
